// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    // 注解的作用，放属性出现在面板中
    // @property(cc.Label)
    // label: cc.Label = null;

    // @property
    // text: string = 'hello';

    // LIFE-CYCLE CALLBACKS:

    // 定义步长
    // @property
    // step: number = 20

    // // 行走的方向
    // @property
    // towardsLeft: boolean = true

    // // 脚步声
    // @property(cc.AudioClip)
    // audio: cc.AudioClip = null

    // onLoad () {
    //   // this 当前组件对象 this.node 当前节点
    //   this.node.on('mousedown', this.moveLeft, this)
    // }

    // moveLeft (evt: cc.Event.EventMouse) {
    //   if (this.towardsLeft) {
    //     this.node.x -= this.step
    //   } else {
    //     this.node.x += this.step
    //   }

    //   // 播放脚步音频
    //   if (this.audio) {
    //     cc.audioEngine.play(this.audio, false, 1)
    //   }

    //   // 获取label组件
    //   const nameNode: cc.Node = cc.find('Canvas/佩奇/name') // 获取节点
    //   nameNode.setPosition(-100, 0)
    //   const label: cc.Label = nameNode.getComponent(cc.Label) // 获取组件
    //   label.string = 'xiao zhu pei qi'
    //   const simpleScript = nameNode.getComponent('SimpleScript')
    //   simpleScript.setY()
    // }

    // start () {
    //   cc.log('组件 PigScript start')
    // }

    // update (dt) {}

    // 速度 每次移动多少像素
    @property
    speed: number = 3

    // 方向 水平方向 （1， 0）竖直向下 （0，-1）
    @property
    direction: cc.Vec2 = null

    onLoad () {
      cc.systemEvent.on('keydown', this.onKeyPress, this)
    }

    onKeyPress (e: cc.Event.EventKeyboard) {
      switch (e.keyCode) {
        case cc.macro.KEY.left:
          this.direction = cc.v2(-1, 0)
          break
        case cc.macro.KEY.right:
          this.direction = cc.v2(1, 0)
          break
        case cc.macro.KEY.up:
          this.direction = cc.v2(0, 1)
          break
        case cc.macro.KEY.down:
          this.direction = cc.v2(0, -1)
          break
        case cc.macro.KEY.space:
          this.direction = null
          break
        default:
          break
      }
    }

    update (dt) {
      if (this.direction === null) return false
      let pos: cc.Vec2 = this.node.getPosition()
      pos.x += this.speed * this.direction.x
      pos.y += this.speed * this.direction.y
      this.node.setPosition(pos)
    }

}
